<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="登录账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['userLoginId',validatorRules.userLoginId]"
            placeholder="请输入登录账号"
            :readOnly="!!model.userLoginId"
          ></a-input>
        </a-form-item>
        <template v-if="!model.userLoginId">
          <a-form-item label="密码" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input
              type="password"
              v-decorator="['currentPassword',validatorRules.currentPassword]"
              placeholder="请输入密码"
            ></a-input>
          </a-form-item>
          <a-form-item label="确认密码" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input
              type="password"
              v-decorator="['currentPasswordVerify',validatorRules.currentPasswordVerify]"
              placeholder="请输入确认密码"
            ></a-input>
          </a-form-item>
        </template>
        <a-form-item label="用户姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name',validatorRules.name]" placeholder="请输入用户姓名"></a-input>
        </a-form-item>
        <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['identityCard']" placeholder="请输入身份证号"></a-input>
        </a-form-item>
        <a-form-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            show-search
            placeholder="请选择所属部门"
            v-decorator="['companyId']"
            style="width: 200px"
            @change="handleChange"
          >
            <a-select-option v-for="(item) in companyList" :key="item.value">{{item.label}}</a-select-option>
          </a-select>
        </a-form-item>
       
        <a-form-item label="昵称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['nickname']" placeholder="请输入昵称"></a-input>
        </a-form-item>
        <a-form-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['phone']" placeholder="请输入手机号码"></a-input>
        </a-form-item>
        <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['email']" placeholder="请输入邮箱"></a-input>
        </a-form-item>
        <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'gender']" placeholder="请选择性别">
            <a-select-option value="M">男</a-select-option>
            <a-select-option value="F">女</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="AD账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['adUserId']" placeholder="请输入AD账号"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk" v-if="!disableSubmit">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import validate from '@/utils/validate'
import JDictSelectTag from "@/components/dict/JDictSelectTag"
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
export default {
  name: 'PerTemporaryTaskModal',
  components: {
    JDictSelectTag,
    JSearchSelectTag,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      dateFormat: 'YYYY-MM-DD',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        userLoginId: {
          rules: [
            { required: true, message: '请输入登录账号!', trigger: 'blur' },
            { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
          ],
        },
        currentPassword: {
          rules: [
            { required: true, message: '请输入密码!', trigger: 'blur' },
            { min: 6, max: 64, message: '长度不能小于6个字符', trigger: 'blur' },
          ],
        },
        currentPasswordVerify: {
          rules: [
            { required: true, message: '请再次输入密码!', trigger: 'blur' },
            { min: 0, max: 64, message: '长度不能小于6个字符', trigger: 'blur' },
           
          ],
        },
        name: {
          rules: [{ required: true, message: '请输入用户姓名!' }],
        },
      },
      url: {
        add: '/online/user/createSysUser',
        edit: '/online/user/updateSysUser',
        companyList: '/online/user/selectAllCompanyTree',
      },
      disableSubmit: false,
      companyList: [],
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.companyList = []
      httpAction(this.url.companyList, {})
      .then((res) => {
        if (res.result.length > 0 ) {
          res.result.forEach((company) => {
            this.companyList.push({ value: company.partyId, label: company.groupName })
          })
        }
      })
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'userLoginId',
            'currentPassword',
            'currentPasswordVerify',
            'name',
            'gender',
            'identityCard',
            'companyId',
            'phone',
            'email',
            'birthDate',
            'adUserId'

          )
        )
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    validateRoleCode(rule, value, callback) {
      if (/[\u4E00-\u9FA5]/g.test(value)) {
        callback('密码不可输入汉字!')
      }
    },
    compareToFirstPassword(rule, value, callback) {
      const form = this.form
      if (value && value !== form.getFieldValue('currentPassword')) {
        callback('两次输入的密码不一样！')
      } else {
        callback()
      }
    },
    handleChange() {},
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.userLoginId) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          formData.isModule = 'CRM'
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          this.model,
          'userLoginId',
          'currentPassword',
          'currentPasswordVerify',
          'name',
          'gender',
          'identityCard',
          'companyId',
          'phone',
          'email',
          'birthDate',
          'adUserId'
        )
      )
    },
  },
}
</script>

<style lang="less" scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 30px;
  margin-bottom: 30px;
  float: right;
}
</style>
